<?php
/**
 * @package mikrokosmos
 * mk script created on 4-ott-2005
 * jaco_at_pixeldump_dot_org
 */

if(!class_exists("MKTagHeader")){ class MKTagHeader {}}


/**
 * html header
 */

class MKHtmlHeader {

	var $docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" />";
	var $htmlTag = "<html>";
	var $head = "<head>";
	var $title = "untitled page";
	var $js = array();
	var $styles = array();

	var $externalJS = array();
	var $externalCSS = array();

	var $body = "<body>";

	var $jsDir = SERVER_MKJS;
	var $cssDir = SERVER_MKCSS;

	// the constructor
	function MKHtmlHeader(){

	}

	//
	function set_docType($docType) { $this->docType = $docType; }
	function get_docType() { return $this->docType; }

	//
	function set_htmlTag($htmlTag) { $this->htmlTag = $htmlTag; }
	function get_htmlTag() { return $this->htmlTag; }

	//
	function set_head($head) { $this->head = $head; }
	function get_head() { return $this->head; }

	//
	function set_title($title) { $this->title = $title; }
	function get_title() { return $this->title; }

	//
	function set_body($body) { $this->body = $body; }
	function get_body() { return $this->body; }

	//
	function add_js($js){ $this->js[] = $js; }
	function get_js(){ return $this->js; }

	//
	function set_styles($styles) { $this->styles = $styles; }
	function get_styles(){ return $this->styles; }
	function add_style($style){ $this->styles[] = $style; }

	//
	function set_externalJS($externalJS) { $this->externalJS = $externalJS; }
	function get_externalJS(){ return $this->externalJS; }
	function add_externalJS($externalJS){ $this->externalJS[] = $externalJS; }

	//
	function set_externalCSS($externalCSS) { $this->externalCSS = $externalCSS; }
	function get_externalCSS(){ return $this->externalCSS; }
	function add_externalCSS($externalCSS){ $this->externalCSS[] = $externalCSS; }


	function fetch_html(){

		$sHtml = "";
		$sHtml .= $this->docType;
		$sHtml .= "\r\n" .$this->htmlTag;
		$sHtml .= "\r\n" .$this->head;
		$sHtml .= "\r\n<title>" .$this->title;
		$sHtml .= "</title>";

		foreach($this->styles as $style){
			$sHtml .= "\r\n" .$style;
		}
		foreach($this->externalCSS as $ec){

			$sHtml .= "\r\n<link rel=\"stylesheet\" type=\"text/css\" href=\"";
			$cssPath = isset($ec["path"]) ? $ec["path"] : $this->cssDir;
			$sHtml .= $cssPath .$ec["cssFileName"] ."\" />";
		}


		foreach($this->js as $js){
			$sHtml .= "\r\n" .$js;
		}
		foreach($this->externalJS as $js){

			$sHtml .= "\r\n<scr" ."ipt language=\"Jav" ."aScrip" ."t\" ";
			$sHtml .= "type=\"text/javascript\" src=\"";
			$jsPath = isset($js["path"]) ? $js["path"] : $this->jsDir;
			$sHtml .= $jsPath .$js["jsFileName"] ."\"></scri" ."pt>";
		}

		$sHtml .= "\r\n" ."</head>";

		if(PD_STATS){
			$sHtml .= "<script type=\"text/javascript\" src=\"http://www.pixeldump.org/pd_stats/php-stats.js.php\">";
		  	$sHtml .= "</script><noscript><img src=\"http://www.pixeldump.org/pd_stats/php-stats.php\" ";
		    $sHtml .= "border=\"0\" alt=\"\" title=\"\"/></noscript>";
		}

		$sHtml .= "\r\n" .$this->body;

		return $sHtml;
	}
}



/**
 * layer class
 */
class MKDiv {

	var $type = "DIV";
	var $cssClass;
	var $style;
	var $divAttribs = array();
	var $content;

	function MKDiv($cssClass = "", $style = "", $content = ""){
		if(strlen($cssClass)) $this->cssClass = $cssClass;
		if(strlen($style)) $this->style = $style;
	}

	function set_type ($type){ $this->type = $type; }
	function get_type() { return $this->type; }

	function set_cssClass ($cssClass){ $this->cssClass = $cssClass; }
	function get_cssClass() { return $this->cssClass; }

	function set_style ($style){ $this->style = $style; }
	function get_style() { return $this->style; }

	function set_content ($content){ $this->content = $content; }
	function get_contet() { return $this->content; }

		//
	function add_divAttrib($attribName, $attribValue){
		$this->divAttribs[$attribName] = $attribValue;
	}


	function fetch_html(){
		$str = "";
		$str .= "<" .$this->type ." ";

		if(strlen($this->cssClass)){
			$str .= "class=\"";
			$str .= $this->cssClass;
			$str .= "\"";
		}

		if(strlen($this->style)){
			$str .= "style=\"";
			$str .= $this->style;
			$str .= "\"";
		}

		if(count($this->divAttribs)){

			foreach($this->divAttribs as $k => $v)
				$str .= " " .$k ."=\"" .$v ."\"";
		}
		$str .= ">\r\n";

		$str .= $this->content;
		$str .= "\r\n</";
		$str .= $this->type .">";

		return $str;
	}
}

/**
 * MAIN TABLE class
 */
class MKTable {

	var $columnHeaders = array();
	var $tableAttribs = array();
	var $tableHeader;
	var $rows = array();

	// the constructor
	function MKTable($rows = array(), $columnHeaders = array(), $columnHeadersAttribs = array()){

		if(count($columnHeaders)){

			$this->columnHeaders = $columnHeaders;
			$ch = array();
			$ch["rowContent"] = array();
			$ch["rowAttribs"] = array();

			foreach($columnHeaders as $columnHeader){
				$idx = count($ch["rowContent"]);
				$ch["rowContent"][$idx] = array();
				$ch["rowContent"][$idx]["cellContent"] = $columnHeader;
			}

			if(count($columnHeadersAttribs)){
				$ch["rowAttribs"] = $columnHeadersAttribs;
			}
			$this->tableHeader = new MKRow($ch["rowContent"], $ch["rowAttribs"]);
		}


		if(count($rows)){
			foreach($rows as $row){
				$idx = count($this->rows);
				$this->rows[$idx] = new MKRow($row["rowContent"]);
				if(isset($row["rowAttribs"]))
					$this->rows[$idx]->set_rowAttribs($row["rowAttribs"]);
			}
		}
	}

	//
	function set_columnHeadersAttribs($columnHeadersAttribs = array()){
		if(!count($columnHeadersAttribs)) return;
		$this->columnHeadersAttribs = $columnHeadersAttribs;
	}

	//
	function add_tableAttrib($attribName, $attribValue){
		$this->tableAttribs[$attribName] = $attribValue;
	}

	//
	function add_headerAttrib($attribName, $attribValue){
		$this->tableHeader->add_rowAttrib($attribName, $attribValue);
	}

	//
	function add_columnHeaderAttrib($attribName, $attribValue, $index = 0){
		$this->tableHeader->add_cellAttribAt($attribName, $attribValue, $index);
	}

	//
	function add_rowAttrib($attribName, $attribValue, $row = 0){
		$this->rows[$row]->add_rowAttrib($attribName, $attribValue);
	}

	//
	function add_cellAttrib($attribName, $attribValue, $row = 0, $col = 0){
		$this->rows[$row]->add_cellAttribAt($attribName, $attribValue, $col);
	}

	//
	function add_row($row = array(), $rowAttribs = array(), $position = -1){
		$index = ($position > -1) ? $position : count($this->rows);

		/* TO DO manage split if position is != -1 */

		$this->rows[$index] = new MKRow($row, $rowAttribs);
		$this->rows[$index]->set_rowAttribs($rowAttribs);
	}

	//
	function get_rowCount(){ return count($this->rows); }

	//
	function fetch_html($nobr = true) {

		$shtml = "<TABLE";

		if(count($this->tableAttribs)){

			foreach($this->tableAttribs as $k => $v)
				$shtml .= " " .$k ."=\"" .$v ."\"";
		}
		$shtml .= ">";

		if(count($this->columnHeaders)) $shtml .= $this->tableHeader->fetch_html();

		foreach($this->rows as $row) $shtml .= $row->fetch_html();

		$shtml .= "</TABLE>" ;
		$shtml .= $nobr ? "" : "\r\n";
		return $shtml;
	}
}

/**
 * TABLE row
 */
class MKRow {

	var $cells = array();
	var $rowAttribs = array();

	// the constructor
	function MKRow($dbcAr = array(), $rowAttribs = array()){

		if(count($dbcAr)){
			foreach($dbcAr as $cell){

				$idx = count($this->cells);
				$this->cells[$idx] = new MKCell($cell["cellContent"]);
				if(isset($cell["cellAttribs"])){
					$this->cells[$idx]->set_cellAttribs($cell["cellAttribs"]);
				}
				if(isset($cell["cellAnonymousAttribs"])){
					$this->cells[$idx]->set_cellAnonymousAttribs($cell["cellAnonymousAttribs"]);
				}
			}
		}
		if(count($rowAttribs)) $this->rowAttribs = $rowAttribs;
	}

	//
	function add_rowAttrib($attribName, $attribValue){
		$this->rowAttribs[$attribName] = $attribValue;
	}

	//
	function set_rowAttribs($rowAttribs = array()){
		if(!count($rowAttribs)) return;
		$this->rowAttribs = $rowAttribs;
	}

	//
	function add_cellAttribAt($attribName, $attribValue, $index = 0){
		$this->cells[$index]->add_cellAttrib($attribName, $attribValue);
	}

	//
	function fetch_html(){

		$shtml = "<TR";

		if(count($this->rowAttribs)){
			foreach($this->rowAttribs as $k => $v)
				$shtml .= " " .$k ."=\"" .$v."\"";
		}

		$shtml .= ">";
		foreach($this->cells as $cell) $shtml .= $cell->fetch_html();
		$shtml .= "</TR>\r\n";

		return $shtml;
	}

}

/**
 * TABLE cell
 */
class MKCell {

	var $cellContent = "&nbsp;";
	var $cellAttribs = array();
	var $cellAnonymousAttribs = array();

	function MKCell($cellContent = "&nbsp;", $cellAttribs = array()){
		$this->cellContent = $cellContent;
		if(count($cellAttribs)){
			$this->cellAttribs = $cellAttribs;
		}
	}

	function set_cellContent($cellContent){
		$this->cellContent = $cellContent;
	}

	function set_cellAttribs($cellAttribs){
		$this->cellAttribs = $cellAttribs;
	}

	function add_cellAttrib($attribName, $attribValue){
		$this->cellAttribs[$attribName] = $attribValue;
	}

	function set_cellAnonymousAttribs($cellAnonymousAttribs = array()){
		$this->cellAnonymousAttribs = $cellAnonymousAttribs;
	}

	function add_anonymousAttrib($attribValue){
		$this->cellAnonymousAttribs[] = $attribValue;
	}

	function fetch_html(){

		$shtml = "<TD";

		if(count($this->cellAttribs)){
			foreach($this->cellAttribs as $k => $v)
				$shtml .= " " .$k ."=\"" .$v."\"";
		}
		if(count($this->cellAnonymousAttribs)){
			foreach($this->cellAnonymousAttribs as $v) $shtml .= " " .$v;
		}

		$shtml .= ">";
		$shtml .= $this->cellContent;
		$shtml .= "</TD>\r\n";

		return $shtml;
	}
}


/**
 * legenda
 */
class MKLegenda {

	var $title = "legenda";
	var $labels = array();
	var $colors = array();

	// the constructor
	function MKLegenda($labels = array(), $colors = array()){

		$cLabels = count($labels);
		$cColors = count($colors);

		if($cLabels && $cColors){
			if($cLabels == $cColors){
				$this->labels = $labels;
				$this->colors = $colors;
			}
		}
	}

	//
	function set_colors($colors = array()){
		$this->colors = $colors;
	}
	function get_colors() { return $this->colors; }

	//
	function set_labels($labels = array()){
		$this->labels = $labels;
	}
	function get_labels() { return $this->labels; }

	function add_itemAt($label = "", $color = "#FFFFFF", $index = -1){

		$idx = $index == -1 ? count($this->colors) : $index;
		$this->colors[$idx] = $color;
		$this->labels[$idx] = $label;
	}

	// orientation:
	// true: vertical
	// false: horizontal
	function fetch_html($orientation = true){

		$tbl = new MKTable();
		$tbl->add_tableAttrib("CLASS", "tblLegenda");

		$cCount = count($this->colors);

		$dbRow = array();
		$dbRow[0] = array();
		$dbRow[0]["cellContent"] = $this->title;

		if($orientation){	// vertical behaviour

			$dbRow[0]["cellAttribs"]["COLSPAN"] = $cCount * 3 + $cCount - 1;
			$tbl->add_row($dbRow);

			for($i = 0; $i < $cCount; $i++){

				$dbRow = array();

				$dbRow[0] = array();
				$dbRow[0]["cellContent"] = "&nbsp;&nbsp;";
				$dbRow[0]["cellAttribs"]["STYLE"] = "background-color:" .$this->colors[$i];

				$dbRow[1] = array();
				$dbRow[1]["cellContent"] = "&nbsp;";

				$dbRow[2] = array();
				$dbRow[2]["cellContent"] = $this->labels[$i];

				if($i < $cCount - 1){
					$dbRow[3] = array();
					$dbRow[3]["cellContent"] = "&nbsp;&nbsp;&nbsp;&nbsp;";
				}

				$tbl->add_row($dbRow);
			}
		}
		else{

			$dbRow[0]["cellAttribs"]["COLSPAN"] = 3;
			$tbl->add_row($dbRow);

			$dbRow = array();
			$k = 0;

			for($i = 0; $i < $cCount; $i++){

				$dbRow[$k] = array();
				$dbRow[$k]["cellContent"] = "&nbsp;&nbsp;";
				$dbRow[$k++]["cellAttribs"]["STYLE"] = "background-color:" .$this->colors[$i];

				$dbRow[$k] = array();
				$dbRow[$k++]["cellContent"] = "&nbsp;";

				$dbRow[$k] = array();
				$dbRow[$k++]["cellContent"] = $this->labels[$i];

				if($k < ($cCount - 1) * 4){
					$dbRow[$k] = array();
					$dbRow[$k++]["cellContent"] = "&nbsp;&nbsp;&nbsp;&nbsp;";
				}
			}
			$tbl->add_row($dbRow);
		}

		$sHtml = $tbl->fetch_html();
		return $sHtml;
	}
}

/**
 * a linkBar object
 */
class MKLinkBar {

	var $links = array();
	var $startElement = "";
	var $endElement = "";
	var $spacer = "&nbsp;";

	// the constructor
	function MKLinkBar($links = array(), $startElement = "", $endElement = "", $spacer = "&nbsp;"){
		if(count($links)) $this->links = $links;
		if(strlen($startElement)) $this->startElement = $startElement;
		if(strlen($endElement)) $this->endElement = $endElement;
		if(strlen($spacer)) $this->spacer = $spacer;
	}

	//
	function set_links($links = array()) { $this->links = $links; }
	function get_links() { return $this->links; }

	//
	function set_startElement($startElement) { $this->startElement = $startElement; }
	function get_startElement() { return $this->startElement; }

	//
	function set_endElement($endElement) { $this->endElement = $endElement; }
	function get_endElement() { return $this->endElement; }

	//
	function set_spacer($spacer) { $this->spacer = $spacer; }
	function get_spacer() { return $this->spacer; }


	function add_linkAt($linkData = array(), $index = -1){
		$idx = $index == -1 ? count($this->links) : $index;
		$this->links[$idx] = new htmlLink($linkData["url"], $linkData["label"]);

		if(isset($linkData["linkAttribs"]))
			$this->links[$idx]->set_linkAttribs($linkData["linkAttribs"]);

		if(isset($linkData["enabled"])) $this->links[$idx]->set_enabled($linkData["enabled"]);


	}

	function fetch_html(){

		$str = "";
		if(strlen($this->startElement))
			$str .= $this->startElement;

		$lCount = count($this->links);

		for($i = 0; $i < $lCount; $i++){
			$str .= $this->links[$i]->fetch_html();

			if($i < $lCount - 1 && strlen($this->spacer))
				$str .= $this->spacer;
		}

		if(strlen($this->endElement))
			$str .= $this->endElement;

		return $str;
	}
}

/**
 * html link class
 */
class MKHtmlLink {

	var $url = "";
	var $label = "link";
	var $linkAttribs = array();
	var $enabled = true;

	// the constructor
	function MKHtmlLink($url = "", $label = "link", $linkAttribs = array()){
		if(strlen($url)) $this->url = $url;
		$this->label = $label;
		if(count($linkAttribs)) $this->linkAttribs = $linkAttribs;
	}

	//
	function set_url($url) { $this->url = $url; }
	function get_url() { return $this->url; }

	//
	function set_label($label) { $this->label = $label; }
	function get_label() { return $this->label; }

	//
	function set_linkAttribs($linkAttribs) { $this->linkAttribs = $linkAttribs; }
	function get_linkAttribs() { return $this->linkAttribs; }

	//
	function set_enabled($flag = true) { $this->enabled = $flag; }
	function get_enabled() { return $this->enabled; }

	function add_linkAttrib($laName, $laValue){
		$this->linkAttribs[$laName] = $laValue;
	}

	function fetch_html(){

		if($this->enabled){

			$str = "<a href=\"";
			$str .= $this->url;
			$str .= "\" ";

			$lCount = count($this->linkAttribs);
			if($lCount){
				$i = 0;
				foreach($this->linkAttribs as $k => $v){
					$str .= $k;
					$str .= "=\"" .$v ."\" ";
				}
			}
			$str .= ">" .$this->label;
			$str .= "</a>";
		}
		else {

			$str = "<SPAN ";

			if(isset($this->linkAttribs["CLASS"])){
				$str .= "CLASS=\"";
				$str .= $this->linkAttribs["CLASS"] ."\"";
			}

			$str .= ">" .$this->label ."</SPAN>";
		}

		return $str;
	}

}
?>
